package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.redis.service.IUserInfRedisService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.Consts;
import cn.wolfcode.wolf2w.util.JsonResult;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("users")
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserInfRedisService redisService;

    @GetMapping("/get")
    public Object get(String id) {
        return userInfoService.get(id);
    }

    @RequireLogin
    @GetMapping("/checkPhone")
    public Object checkPhone(String phone) {
        boolean ret = userInfoService.checkPhone(phone);
        return !ret;
    }

    //验证码
    @RequireLogin
    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone) {
        redisService.setCount();

        String code = UUID.randomUUID().toString().replace("-", "").substring(0, 4);
        StringBuilder sb = new StringBuilder(80);
        //int i = 1/0;
        sb.append("亲,您注册骡窝窝的验证码为:").append(code).append(" 验证码将在").
                append(Consts.VERIFY_CODE_VAI_TIME).append("分钟后失效,请注意时间哦!");
        /*String url = "https://way.jd.com/chuangxin/dxjk?mobile={1}&content={2}&appkey={3}";
        String appkey = "22c9c1eac67369ade124e0cb4244897b";
        RestTemplate restTemplate = new RestTemplate();
        String ret = restTemplate.getForObject(url,String.class, phone, sb.toString(), appkey);
        System.out.println(ret);
        System.out.println(phone);*/
        System.out.println(sb);
        redisService.setVerifyCode(phone, code);
        return JsonResult.success();
    }

    //注册
    @RequireLogin
    @PostMapping("/regist")
    public Object regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        /*try {
            userInfoService.regist(phone, nickname, password, rpassword, verifyCode);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error(JsonResult.CODE_ERROR_PARAM, e.getMessage(), null);
        }*/
        userInfoService.regist(phone, nickname, password, rpassword, verifyCode);
        return JsonResult.success();
    }

    //登录
    @RequireLogin
    @PostMapping("/login")
    public Object login(String username, String password) {
        HashMap<String, Object> map = new HashMap<>();
        /*try {
            UserInfo user = userInfoService.login(username, password);
            String token = redisService.setToken(JSON.toJSONString(user));
            map.put("token", token);
            map.put("user", user);
        } catch(Exception e) {
            e.printStackTrace();
            return JsonResult.error(JsonResult.CODE_ERROR_PARAM, e.getMessage(), null);
        }*/
        UserInfo user = userInfoService.login(username, password);
        String token = redisService.setToken(JSON.toJSONString(user));
        map.put("token", token);
        map.put("user", user);
        return JsonResult.success(map);
    }


    @RequireLogin
    @GetMapping("/currentUser")
    public Object currentUser(HttpServletRequest request) {
        String token = request.getHeader("token");
        UserInfo user = redisService.getUserByToken(token);
        return JsonResult.success(user);
    }

    @RequireLogin
    @GetMapping("/strategies/favor")
    public Object user_favor(String userId) {
        List<String> uidList = userInfoService.getUserFavor(userId);
        return JsonResult.success(uidList);
    }


}
